#include "jdatafile.h"


JDataFile::JDataFile()
{
    CurrentPath = GetCurrentPath();
    DataList.clear();
    GetAllLines("JDataFile.jdf", DataList);
}

std::string JDataFile::GetCurrentPath()
{
    char Path[MAXPATH];
    memset(Path, 0, MAXPATH);
    getcwd(Path, MAXPATH);
    return Path;
}

int JDataFile::GetAllLines(const std::string FileName, std::deque<std::string> &DataList)
{
    int Cnt = 0;
    std::string Path = CurrentPath + "/" + FileName;
    std::ifstream in(Path.c_str());
    if(!in)
        return Cnt;
    std::string line;
    while(std::getline(in, line)) {
        ++Cnt;
        DataList.push_back(line);
        std::cout << line << std::endl;
    }
    in.close();
    return Cnt;
}

int JDataFile::GetData(std::deque<std::string> &List)
{
    List = DataList;
    return List.size();
}

bool JDataFile::AddLineData(const std::string FileName, const std::string Data)
{
    std::string Path = CurrentPath + "/" + FileName;
    DataList.push_back(Data);
    std::ofstream Writer(Path.c_str(), std::ios::app | std::ios::binary);
    Writer << Data << std::endl;
    Writer.close();
    return true;
}

bool JDataFile::ModifiedLineData(const std::string FileName, const std::string Data)
{
    // 删除数据文件
    std::string Path = CurrentPath + "/" + FileName;
    std::remove(Path.c_str());
    // 删除队列中的一行数据并保存剩下的数据
    for(unsigned int i = 0; i < DataList.size(); i++) {
        if(Data == DataList.at(i)) {
            DataList.at(i) = Data;
        }
        AddLineData(FileName, DataList.at(i));
    }
    return true;
}

bool JDataFile::ModifiedLineData(const std::string FileName, const unsigned int Index, const std::string Data)
{
    // 修改数据队列中数据
    if(DataList.size() > Index)
        DataList.at(Index) = Data;
    else
        return false;
    // 删除数据文件
    std::string Path = CurrentPath + "/" + FileName;
    std::remove(Path.c_str());
    // 删除队列中的一行数据并保存剩下的数据
    for(unsigned int i = 0; i < DataList.size(); i++) {
        AddLineData(FileName, DataList.at(i));
    }
    return true;
}

bool JDataFile::DeleteLineData(const std::string FileName, const std::string Data)
{
    // 删除数据文件
    std::string Path = CurrentPath + "/" + FileName;
    std::remove(Path.c_str());
    // 删除队列中的一行数据并保存剩下的数据
    for(unsigned int i = 0; i < DataList.size(); i++) {
        if(Data == DataList.at(i)) {
            DataList.erase(DataList.begin() + i);
            --i;
        }
        else {
            AddLineData(FileName, DataList.at(i));
        }
    }
    return true;
}

bool JDataFile::DeleteLineData(const std::string FileName, const unsigned int Index)
{
    // 删除数据队列中数据
    if(DataList.size() > Index)
        DataList.erase(DataList.begin() + Index);
    else
        return false;
    // 删除数据文件
    std::string Path = CurrentPath + "/" + FileName;
    std::remove(Path.c_str());
    // 删除队列中的一行数据并保存剩下的数据
    for(unsigned int i = 0; i < DataList.size(); i++) {
        AddLineData(FileName, DataList.at(i));
    }
    return true;
}

bool JDataFile::DeleteTypeData(const std::string FileName, std::string Type)
{
    DataList.clear();
    std::deque<std::string> List;
    GetAllLines(FileName, List);
    DeleteDataFile(FileName);
    for(unsigned int i = 0; i < List.size(); i++) {
        std::string line = List.at(i);
        std::string str = line.substr(0, line.find(":"));
        if(Type != str && !line.empty()) {
            DataList.push_back(line);
            AddLineData(FileName, line);
        }
    }
    return true;
}

bool JDataFile::DeleteDataFile(const std::string FileName)
{
    std::string Path = CurrentPath + "/" + FileName;
    std::remove(Path.c_str());
    return true;
}

bool JDataFile::SaveAllData(const std::string FileName, const std::deque<std::string> List)
{
    if(List.size() <= 0)
        return false;
    for(unsigned int i = 0; i < DataList.size(); i++) {
        AddLineData(FileName, DataList.at(i));
    }
    return true;
}

std::string JDataFile::PwdtoString(JDPasswdNote Data)
{
    std::string Value = "Passwd:";
    Value = Value + EncryptionString(Data.CreateTime) + ",";
    Value = Value + EncryptionString(Data.ModifiedTime) + ",";
    Value = Value + EncryptionString(Data.Name) + ",";
    Value = Value + EncryptionString(Data.Type) + ",";
    Value = Value + EncryptionString(Data.Account) + ",";
    Value = Value + EncryptionString(Data.Email) + ",";
    Value = Value + EncryptionString(Data.Phone) + ",";
    Value = Value + EncryptionString(Data.Pwd) + ",";
    Value = Value + EncryptionString(Data.Pwd2) + ",";
    Value = Value + EncryptionString(Data.Remark);
    return Value;
}

JDPasswdNote JDataFile::StringtoPwd(std::string Data)
{
    JDPasswdNote Pwd;
    Data = Data.substr(Data.find(":") + 1, Data.length());
    Pwd.CreateTime = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Pwd.ModifiedTime = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Pwd.Name = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Pwd.Type = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Pwd.Account = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Pwd.Email = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Pwd.Phone = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Pwd.Pwd = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Pwd.Pwd2 = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Pwd.Remark = DecodeString(Data.substr(0, Data.find(",")));
    return Pwd;
}

std::string JDataFile::BooktoString(JDBookData Data)
{
    std::string Value = "Books:";
    Value = Value + EncryptionString(Data.BookName) + ",";
    Value = Value + EncryptionString(Data.BookType) + ",";
    Value = Value + EncryptionString(Data.BookAuthor) + ",";
    Value = Value + EncryptionString(Data.AuthorHome) + ",";
    Value = Value + EncryptionString(Data.BookPunlish) + ",";
    Value = Value + EncryptionString(Data.DataTime) + ",";
    Value = Value + EncryptionString(Data.BookStatus) + ",";
    Value = Value + EncryptionString(Data.TimeLen) + ",";
    Value = Value + EncryptionString(Data.BookSummary) + ",";
    Value = Value + EncryptionString(Data.BookPlat) + ",";
    Value = Value + EncryptionString(Data.Remark);
    return Value;
}

JDBookData JDataFile::StringtoBook(std::string Data)
{
    JDBookData Book;
    Data = Data.substr(Data.find(":") + 1, Data.length());
    Book.BookName = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.BookType = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.BookAuthor = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.AuthorHome = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.BookPunlish = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.DataTime = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.BookStatus = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.TimeLen = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.BookSummary = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.BookPlat = DecodeString(Data.substr(0, Data.find(",")));
    Data = Data.substr(Data.find(",") + 1, Data.length());
    Book.Remark = DecodeString(Data.substr(0, Data.find(",")));
    return Book;
}


std::string JDataFile::EncryptionString(std::string Data)
{
    for(unsigned int i = 0; i < Data.size(); i++) {
        Data.at(i) += 1;
    }
    return Data;
}

std::string JDataFile::DecodeString(std::string Data)
{
    for(unsigned int i = 0; i < Data.size(); i++) {
        Data.at(i) -= 1;
    }
    return Data;
}

